import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:schedule_self_client/common/api_common.dart';
import 'package:schedule_self_client/native/app_install.dart';
import 'package:schedule_self_client/state_management/controller/new_app_download_controller.dart';

class ToolApi {
  final NewAppDownloadController controller =
      Get.put(NewAppDownloadController());

  static final Dio _dio = Dio(BaseOptions(
    baseUrl: ApiCommon.baseUrl,
    headers: {},
  ));

  static init(InterceptorsWrapper interceptorsWrapper) {
    _dio.interceptors.add(interceptorsWrapper);
  }

  Future<void> downloadApp(
      String url, String filename, BuildContext context) async {
    // final externalDir = await getExternalStorageDirectory();
    // String savePath = '${externalDir?.path}/$filename';
    String savePath = '$filename';
    await _dio.download(url, savePath,
        onReceiveProgress: (int count, int total) async {
      controller.updateCount(count, total);
      if (total == count) {
        await AppInstall.instance().install(savePath);
      }
    });
  }
}
